//
//  ZWGetDataTool.swift
//  SwiftFreshBeen
//
//  Created by Apple on 16/12/23.
//  Copyright © 2016年 zhouwnegang. All rights reserved.
//

import UIKit
import YYModel

class ZWGetDataTool: NSObject {
    
    //创建一个获取模型数据的单例
    static let shared = ZWGetDataTool()
    
    //获取首页数据
    func getHomeData(completedClosure: @escaping (_ homedata:ZWHomeData?,_ isSucess:Bool)->()) {
        
        let urlString = "http://iosapi.itcast.cn/loveBeen/focus.json.php"
        let parameter:[String:Any] = ["call":1]
        
        HMNetworkingTools.shared.request(method: .POST, URLString: urlString, parameters: parameter) { (respondObject, error) in
            
            //            var homeArray = [Any]()
            
            let homeData = ZWHomeData()
            
            if error != nil  {
                
                print("数据请求失败")
                completedClosure(nil,false)
                return
                
            }
            
            //数据请求成功
            let respondDict = try? JSONSerialization.jsonObject(with: respondObject as! Data , options: JSONSerialization.ReadingOptions.allowFragments) as! [String:Any]
            
            //获取活动数据
            let dataDict =  respondDict?["data"] as! [String:Any]
            let activitiesArray = dataDict["activities"] as! [Any]
            let focusArray = dataDict["focus"] as! [Any]
            let iconsArray = dataDict["icons"] as! [Any]
            
            //转为活动模型
            let  activities = NSArray.yy_modelArray(with: ZWActivityModel.self, json: activitiesArray)
            let  focus = NSArray.yy_modelArray(with: ZWFocusModel.self, json: focusArray)
            let  icons = NSArray.yy_modelArray(with: ZWIconsModel.self, json: iconsArray)
            
            homeData.activities = activities as! [ZWActivityModel]?;
            homeData.focus = focus as! [ZWFocusModel]?;
            homeData.icons = icons as! [ZWIconsModel]?;
            
            completedClosure(homeData,true)
            
        }
        
    }
    
    //新鲜热卖
    func getHomeHotSaleData(completedClosure: @escaping (_ homedata:[ZWHomeHotSaleModel]?,_ isSucess:Bool)->()){
        
        let urlString = "http://iosapi.itcast.cn/loveBeen/firstSell.json.php"
        let parameter:[String:Any] = ["call":2]
        
        //请求数据
        HMNetworkingTools.shared.request(method: .POST, URLString: urlString, parameters: parameter) { (repondObject, error) in
            
            if error != nil {
                completedClosure(nil,false)
                return
            }
            //数据请求成功
            let respondDict = try? JSONSerialization.jsonObject(with: repondObject as! Data , options: JSONSerialization.ReadingOptions.allowFragments) as! [String:Any]
            
            let dataDict =  respondDict?["data"] as! [Any]
            //字典转模型
            let modelArray =  NSArray.yy_modelArray(with: ZWHomeHotSaleModel.self, json: dataDict)
            
            completedClosure(modelArray as! [ZWHomeHotSaleModel]?,true)
            
        }
    }
    
    
    
    //获取程序启动时的广告图片
    func getStartAdData(completedClosure: @escaping (_ avModel:ZWStartAdModel?,_ isSucess:Bool)->()){
        
        
        let urlString = "http://iosapi.itcast.cn/loveBeen/ad.json.php"
        let parameter:[String:Any] = ["call":7]
        
        //请求数据
        HMNetworkingTools.shared.request(method: .POST, URLString: urlString, parameters: parameter) { (repondObject, error) in
            
            if error != nil {
                completedClosure(nil,false)
                return
            }
            //数据请求成功
            let respondDict = try? JSONSerialization.jsonObject(with: repondObject as! Data , options: JSONSerialization.ReadingOptions.allowFragments) as! [String:Any]
            
            let dataDict =  respondDict?["data"]
            
            print("%@",dataDict!)
            
            //字典转模型
            let obj =  ZWStartAdModel.yy_model(withJSON: dataDict!)
            
            completedClosure(obj,true)
            
        }
    }
}
